package thread;

//  可重入锁
class Count2{
    public int count = 0;
    synchronized public void add() {
        count++;
    }
}

public class demo17_synchronized {
    public static void main(String[] args) throws InterruptedException {

        Count2 c = new Count2();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (c) {
                    c.add();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (c) {
                    c.add();
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(c.count);
    }
}
